package com.qo.qsdk.ui.base.floater;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.Window;

import com.qo.qsdk.ui.QSDKListener;
import com.qo.qsdk.utils.DynamicUtils;
import com.qo.qsdk.utils.Language;
import com.qo.qsdk.utils.QSDKHelper;

import java.util.ArrayList;

/**
 * @author Anmi on 2018/4/26.
 */

public class FloatButtonUtil {

    static FloatLogoMenu mFloatMenu;
    static Activity mActivity;
    static boolean mHasPay;
    static boolean mHasCustomer;

    public static void initFloatView(Activity activity) {
        mActivity = activity;
        mHasPay = QSDKHelper.getInstance().getConfigureModel().getInitModel().hasRec();
        mHasCustomer = QSDKHelper.getInstance().getConfigureModel().getInitModel().hasCs();
        ArrayList<FloatItem> itemList = new ArrayList<>();
        if (mHasPay) {
            itemList.add(addItem(activity, Language.Float_Pay.toString(), DynamicUtils.mipmap(activity, "qsdk_f_p"), "", QSDKHelper.dip2px(activity, 23), QSDKHelper.dip2px(activity, 24)));
        }
        if (QSDKHelper.getInstance().getConfigureModel().getInitModel().hasIco()) {
            itemList.add(addItem(activity, Language.Float_Customer.toString(), DynamicUtils.mipmap(activity, "qsdk_f_s"), "",QSDKHelper.dip2px(activity, 29),QSDKHelper.dip2px(activity, 23)));
        }
        itemList.add(addItem(activity, Language.Float_Close.toString(),  DynamicUtils.mipmap(activity, "qsdk_f_c"), "",QSDKHelper.dip2px(activity, 24),QSDKHelper.dip2px(activity, 24)));

        //点击屏幕时关闭展开的悬浮菜单
        Window win = activity.getWindow();
        win.setCallback(new SimpleWinCallback(win.getCallback()) {
            @Override
            public boolean dispatchTouchEvent(MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (mFloatMenu != null) mFloatMenu.hide();
                }
                return super.dispatchTouchEvent(event);
            }
        });
        //先隐藏
        destroyFloat();
        init(activity, itemList);
    }

    private static FloatItem addItem(Activity activity, String title, int resImgId, String endPath, int w, int h) {
        FloatItem floatItem = new FloatItem(title, Color.parseColor("#ED6E3D"), 0x00000000,
                BitmapFactory.decodeResource(activity.getResources(), resImgId), String.valueOf(0), endPath, w, h);
        return floatItem;
    }

    public static boolean isVisible() {
        return mFloatMenu != null && mFloatMenu.isShow();
    }

    private static void init(final Activity activity, final ArrayList<FloatItem> itemList) {
        if (mFloatMenu == null) {

            mFloatMenu = new FloatLogoMenu.Builder()
                    .withActivity(activity)
//                    .withContext(mActivity.getApplication())//这个在7.0（包括7.0）以上以及大部分7.0以下的国产手机上需要用户授权，需要搭配<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
                    .logo(BitmapFactory.decodeResource(activity.getResources(), DynamicUtils.mipmap(activity, "qsdk_f_logo_l")))
                    .drawCicleMenuBg(false)//将图片切成圆形
                    .backMenuColor(0xffffff)//菜单背景颜色
                    .setBgDrawable(activity.getResources().getDrawable(DynamicUtils.drawable(activity, "qsdk_f_bg")))
                    //这个背景色需要和logo的背景色一致
                    .setFloatItems(itemList)
                    .defaultLocation(FloatLogoMenu.LEFT)//按钮默认悬浮的位置
                    .drawRedPointNum(false)//是否绘制红点数字
                    .showWithListener(new FloatMenuView.OnMenuClickListener() {
                        @Override
                        public void onItemClick(int position, String title) {
                            if (position == itemList.size() - 1) {
                                if (mIAccountListener != null) {
                                    mIAccountListener.onFloatClose();
                                }
                                hide();
                            }else {
                                if (title.equals(Language.Float_Customer.toString())) {
                                    if (mIAccountListener != null) {
                                        mIAccountListener.onFloatCustomer();
                                    }
                                    closeMenu();
                                    return;
                                }
                                if (title.equals(Language.Float_Pay.toString())) {
                                    if (mIAccountListener != null) mIAccountListener.onFloatPay();
                                    closeMenu();
                                    return;
                                }
                            }
                        }

                        @Override
                        public void dismiss() {
//                            hide();
                        }
                    });
        }
    }

    public static void hide() {
        if (mFloatMenu != null)
            mFloatMenu.destoryFloat();
    }

    public static void closeMenu() {
        if (mFloatMenu != null)
            mFloatMenu.hide();
    }

    static QSDKListener mIAccountListener;
    public static void show(Activity activity, QSDKListener mIAccountListener2) {
        mIAccountListener = mIAccountListener2;
        if (mFloatMenu == null) initFloatView(activity);
        if (!mFloatMenu.isShow()) mFloatMenu.show();
    }

    public static void destroyFloat() {
        if (mFloatMenu != null) {
            mFloatMenu.destoryFloat();
        }
        mFloatMenu = null;
    }

    public static boolean isInit() {
        return mFloatMenu != null;
    }
}
